// pages/chargeDetail/changeDetail.js
const app = getApp();
import {
    queryUrl
} from '../../utils/util'

const $api = require('../../api/index')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        dataList: [],
        navTop: app.globalData.navTop,
        detail: '',
        totalCount: 0,
        page: 1,
        size: 10,
        terminalStatus: { //0离线1故障2空闲3充电8预约
            0: '../../static/images/chargeDetail-offline.png',
            1: '../../static/images/chargeDetail-fault.png',
            2: '../../static/images/chargeDetail-idle.png',
            3: '../../static/images/chargeDetail-chargings.png',
            8: '../../static/images/chargeDetail-reservation.png',
        }
    },
    openMap(e) { //打开地图
        const latitude = parseFloat(this.data.detail.latitude)
        const longitude = parseFloat(this.data.detail.longitude)
        const name = this.data.detail.name
        const address = this.data.detail.address
        wx.openLocation({
            latitude,
            longitude,
            scale: 18,
            name: name,
            address: address
        })
    },
    goBack() { //返回
        wx.navigateBack()
    },
    gotoAdvancePayment({
        mark
    }) { //跳转预支付
        if (mark.status === 2) {
            wx.navigateTo({
                url: '../advancePayment/index?terminal_no=' + mark.id
            })
        }
    },
    scanCode() {
        wx.scanCode({
            success: function (res) {
                const terminal_no = queryUrl(res.result).terminal_no;
                wx.navigateTo({
                    url: '/pages/advancePayment/index?terminal_no=' + terminal_no,
                });
            }
        })
    },
    gotoReservation({
        mark
    }) { //跳转预约
        wx.navigateTo({
            url: `../appointmentOrder/appointmentOrder?id=${mark.id}`
        })
    },
    nextPage() { //下一页
        this.setData({
            page: this.data.page + 1
        })
        this.getTerminalList()
    },
    gotoPriceDetail({
        mark
    }) { //跳转价格详情
        wx.navigateTo({
            url: '../chargingUnitPrice/index?id=' + mark.id
        })
    },
    gotoCustomerService() { //跳转客服
        wx.navigateTo({
            url: '../customerService/customerService'
        })
    },
    getTerminalList() { //站点终端列表
        let data = {
            org_id: this.data.id,
            page: this.data.page,
            pageSize: this.data.size

        }
        $api.terminalList(data).then((res) => {
            res.data.terminalList.map(item => {
                item.currTerminalPriceVO.totalPrice = (Number(item.currTerminalPriceVO.price) + Number(item.currTerminalPriceVO.service_price)).toFixed(4)
            })
            let list = this.data.dataList.concat(res.data.terminalList)
            this.setData({
                dataList: list,
                totalCount: res.data.total_count
            })
        })
    },
    getList(id) { //电站详情
        let data = {
            longitude: wx.getStorageSync('longitude'),
            latitude: wx.getStorageSync('latitude'),
            org_id: id,
        }
        $api.orgDetail(data).then((res) => {
            res.data.distance = (res.data.distance / 1000).toFixed(2)
            this.setData({
                detail: res.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: this.options.id
        })
        this.getList(options.id)
        this.getTerminalList(options.id)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})